/*
 * CS_REQ_COMBINE_BATTLE_PET.cpp
 *
 *  Created on: 2013. 8. 6.
 *      Author: mega@gamevil.com
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/Item.h"
#include "../../NResource/Static.h"
#include "../../NLogic/VirtualCurrency.h"
#include "../../GameLog.h"

namespace ProjectZ { namespace NCommand {

void SendFailBattlepetCombine(User * pUser, int res)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(res);
	pAck->MakePacket(CMD_SC_REQ_COMBINE_BATTLE_PET);
	pUser->Send(pAck);
}

void Command::CS_REQ_COMBINE_BATTLE_PET(User * pUser, ::Network::Packet * pPacket)
{
/*	CS_REQ_COMBINE_BATTLE_PET	0x3002
	{
		u64 u64base_pet;			//합성할 펫
		u64 u64material_pet;		//재료
		u8 u8CombineType; 			//0: 일반조합 (골드소모), 1: 특수조합 (젠소모)
	}

	SC_REQ_COMBINE_BATTLE_PET	0x3003
	{
		s16	u16ack;				//0: 실패, 1: 합성 성공
	}*/

	PREPARE_PACKET;

	uint64_t basePetSeq = 0;
	uint64_t materialPetSeq = 0;
	char combineType = 0;

	*pPacket >> basePetSeq >> materialPetSeq >> combineType;

	NLogic::Bag * pBag = pUser->GetBag(BAG_TYPE_BATTLE_PET);
	if (pBag == NULL)
	{
		SendFailBattlepetCombine(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NLogic::Item * pItemBase		= pBag->GetItem(basePetSeq);
	NLogic::Item * pItemMaterial	= pBag->GetItem(materialPetSeq);

	if (pItemBase == NULL || pItemMaterial == NULL)
	{
		SendFailBattlepetCombine(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	// 금액 확인
	int needGold	= 0;
	int needZEN		= 0;

	if (0 == combineType)
	{
		needGold = pow(pItemBase->GetData().quality + 1, 2) * 1000;
		if (needGold > static_cast<int>(pUser->GetCharacterInfo().gold))
		{
			SendFailBattlepetCombine(pUser, ACK_NOT_ENOUGH_GOLD);
			return;
		}
	}
	else
	{
		needZEN = (pItemBase->GetData().quality + 1) * 10;
		if (needZEN > pUser->GetVirtualCurrency()->GetZen())
		{
			SendFailBattlepetCombine(pUser, ACK_NOT_ENOUGH_ZEN);
			return;
		}
	}

	NLogic::Item::Data & clsDataBase		= pItemBase->GetData();
	NLogic::Item::Data & clsDataMaterial	= pItemMaterial->GetData();

	// 전투펫 합성
	int ackReturn = NResource::Static::InstancePtr()->GetBattlepetGachya()->BattlePetCombine(pUser, clsDataBase, clsDataMaterial, combineType);

	if (ACK_BATTLEPET_COMBINE_FAIL != ackReturn && ACK_OK != ackReturn)
	{
		SendFailBattlepetCombine(pUser, ackReturn);
		return;
	}

	// 재료펫 삭제
	pBag->PopItem(materialPetSeq);
	pItemMaterial->DeleteDatabaseItemInfo(NLogic::Item::ItemStatus::EM_ITEM_STATUS_BATTLEPET_COMBINE);

	// 기준펫 갱신
	// 전투펫은 4시간 마다 스태미너가 감소하므로, 합성을 통해 스태미너가 회복되는 경우 시간 갱신
	pItemBase->GetData().reg_date = time(NULL);
	pItemBase->UpdateDatabaseItemInfo();

	// 응답
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ackReturn);
	pAck->MakePacket(CMD_SC_REQ_COMBINE_BATTLE_PET);
	pUser->Send(pAck);

	// VC 처리
	if (0 == combineType)
	{
		NLogic::NFunction::ModifyGold(pUser, needGold * -1);
		NGameLog::LogGoldInfo(pUser, BATTLE_PET, needGold * -1);
	}
	else
	{
		NLogic::NFunction::VCProcess(pUser, EM_VCINDEX_BATTLEPET_COMBINE, needZEN);
	}
}

} /* NCommand */ } /* ProjectZ */
